<template>
  <div class="section-wrap container" v-wechat-title="$route.meta.title + '-' + title">
    <div class="section-title">确认订单</div>
    <el-row type="flex" align="middle" justify="space-between" class="section-tab">
      <span>课程详情</span>
      <span>售价</span>
    </el-row>
    <div class="section-buy" v-if="!$util.isEmpty(orderInfo)">
      <!-- 优惠卷 -->
      <div class="pay-coupons">
        <div class="pay-coupons-list">
          <el-row type="flex" justify="space-between" class="item">
            <template v-if="orderInfo.course_type == 1">
              <el-tag
                class="type video"
                effect="plain"
                size="mini"
                v-if="orderInfo.data_type == 1"
                >{{ orderInfo.data_type_text }}</el-tag
              >
              <el-tag
                class="type audio"
                effect="plain"
                size="mini"
                v-else-if="orderInfo.data_type == 2"
                >{{ orderInfo.data_type_text }}</el-tag
              >
              <el-tag
                class="type text"
                effect="plain"
                size="mini"
                v-else-if="orderInfo.data_type == 3"
                >{{ orderInfo.data_type_text }}</el-tag
              >
              <el-tag
                class="type book"
                effect="plain"
                size="mini"
                v-else-if="orderInfo.data_type == 4"
                >{{ orderInfo.data_type_text }}</el-tag
              >
            </template>
            <el-row type="flex" justify="space-between" class="col">
              <div class="name">{{ orderInfo.title }}</div>
              <div class="count" v-if="orderInfo.term_time === 0">
                永久有效
              </div>
              <div class="count" v-else-if="('' + orderInfo.term_time).length < 5">
                购买之日起<span>{{ orderInfo.term_time }}</span
                >天内有效
              </div>
              <div class="count" v-else>
                {{ orderInfo.term_time | parseTime('{y}年{m}月{d}日前有效') }}
              </div>
            </el-row>
            <div class="pirce">
              {{ orderInfo.price | formatMoney }}
            </div>
          </el-row>
        </div>
      </div>
      <!-- 积分抵扣 -->
      <Integral :credit_arr="credit_arr" :value="credit_price" @change="changePoints"></Integral>
      <el-divider></el-divider>
      <!-- 付款信息 -->
      <el-row class="pay-buy" type="flex" align="middle" justify="end">
        <el-row type="flex" justify="space-between" class="money">
          <el-row type="flex" align="middle" justify="space-between">
            <div class="text">合计</div>
            <div class="price">{{ total_price | formatMoney }}</div>
          </el-row>
        </el-row>
        <el-button type="primary" @click="handleOrder" :disabled="disabled">提交订单</el-button>
      </el-row>
    </div>
  </div>
</template>

<script>
import projectSetting from '@/settings/projectSetting.js'
import { mapMutations } from 'vuex'
import Integral from './components/integral'
export default {
  components: {
    Integral
  },
  data() {
    return {
      orderInfo: {},
      section_id: this.$route.query.sid,
      disabled: false,
      total_price: 0,
      credit_arr: [],
      credit_price: 0,
      creditObj: {}
    }
  },
  created() {
    this.getOrderInfo()
  },
  computed: {
    title() {
      return this.orderInfo.title || ''
    }
  },
  methods: {
    ...mapMutations({
      changeOrderData: 'order/SET_ORDER_DATA'
    }),
    // 获取订单信息
    getOrderInfo() {
      this.$api.getOrderSection(
        {
          section_id: this.section_id
        },
        res => {
          const { data } = res
          this.total_price = data.user_price
          this.orderInfo = data
          this.credit_arr = data.credit_arr
          this.changePoints(data.credit_arr[0])
        },
        error => {
          this.$message.error(error.msg)
        }
      )
    },
    // 提交订单
    handleOrder() {
      let _this = this
      _this.disabled = true
      let query = {
        from: projectSetting.globDeviceType,
        section_id: _this.section_id.toString()
      }
      if (!this.$util.isEmpty(_this.creditObj)) {
        query = {
          ...query,
          credit: JSON.stringify(this.creditObj)
        }
      }
      _this.$api.createOrderSection(
        query,
        res => {
          if (res.data.need_pay) {
            _this.changeOrderData({
              order_no: res.data.order_no,
              payment: res.data.payment,
              return_url: _this.$route.query.redirect
            })
            _this.$router.push({
              path: '/buy/confirm'
            })
          } else {
            _this.$message.success(res.msg)
            _this.$router.replace({
              path: this.$route.query.redirect
            })
          }
        },
        error => {
          _this.disabled = false
          _this.$message.error(error.msg)
        }
      )
    },
    // 积分抵扣
    changePoints(obj) {
      let credit = 0
      if (!this.$util.isEmpty(obj)) {
        credit = obj.num
      }
      this.creditObj = obj
      this.credit_price = credit
      this.total_price = this.orderInfo.user_price - credit
    }
  }
}
</script>

<style lang="scss" scoped>
.section-wrap {
  margin-top: 10px;
  margin-bottom: 20px;
  padding-bottom: 40px;
  background-color: #ffffff;
  .section-title {
    padding-left: 30px;
    height: 44px;
    font-size: 16px;
    font-weight: 500;
    color: $text-color-import;
    line-height: 44px;
  }
  .section-tab {
    padding: 0 56px 0 210px;
    margin: 0 6px;
    height: 44px;
    font-size: 14px;
    font-weight: 500;
    color: $text-color-secondary;
    background: rgba(246, 249, 255, 1);
  }
}
.section-buy {
  margin-top: 20px;
  .pay-coupons {
    padding: 0 30px;
    .pay-coupons-list {
      margin-top: 10px;
      padding: 2px 20px 22px;
      border: 1px solid rgba(235, 238, 245, 1);
      .item {
        margin-top: 20px;
        .type {
          border-radius: 0;
          border-top-left-radius: 8px;
          border-bottom-right-radius: 8px;
          &.video {
            @include edu-color-primary(border-color);
            @include edu-color-auxiliary;
          }
          &.audio {
            border-color: #67c23a;
            color: #67c23a;
          }
          &.text {
            border-color: #783ce6;
            color: #783ce6;
          }
          &.book {
            border-color: #e6a23c;
            color: #e6a23c;
          }
        }
        .col {
          flex: 1;
          flex-direction: column;
          margin-left: 10px;
          padding-bottom: 15px;
        }
        .name {
          font-size: 13px;
          font-weight: 500;
          color: $text-color-import;
          line-height: 18px;
          margin-bottom: 20px;
        }
        .count {
          font-size: 12px;
          color: $text-color-secondary;
        }
        .pirce {
          font-size: 14px;
          font-weight: 500;
          color: $text-color-price;
        }
      }
    }
  }
  .pay-buy {
    padding: 0 30px;
    .money {
      flex-direction: column;
      width: 130px;
      margin-right: 40px;
      .text {
        font-size: 13px;
        font-weight: 400;
        color: $text-color-secondary;
      }
      .price {
        font-size: 18px;
        font-weight: 500;
        color: $text-color-price;
      }
    }
  }
}
</style>
